package testes;


import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import cflow.Cflow;
@RunWith(Parameterized.class) 
public class teste {
	static String testFile="testfile.csv";
	static dadosTeste dado;
	public boolean resultados(String regex,  LinkedList<String> input)
	{ 
		
		regex=regex+'\n';
		Cflow c = new Cflow(regex,1,1,false);
		for (String values :input)
		c.addTransition(values);
	 
		if(c.getCurrentState()==null)
			return false;
		if(!c.getCurrentState().isFinal())
			return false;
		if(c.getAcceptedTrace().size()!=input.size())
			return false;
		for (int i=0;i<input.size();i++)
		{
			if(!input.get(i).equals(c.getAcceptedTrace().get(i)))
				return false;
		}
		
		return true;
		
	}
	public teste(String regex, LinkedList<String> input, boolean result) {dado=new dadosTeste(regex, input, result);}  
	
	 @Parameters  
     public static Collection<Object[]>
                                generateParams() {  
          List<Object[]> params =
                  new ArrayList<Object[]>();  
        
                  try {
          			String expression, transitions, evaluationResult;
          			@SuppressWarnings("unused")
					boolean res = false;
          			@SuppressWarnings("resource")
					BufferedReader br = new BufferedReader(new FileReader(testFile));
          			while ((expression = br.readLine()) != null && (transitions = br.readLine()) != null && ((evaluationResult = br.readLine()) != null)) {		
          			
          				String[] split = transitions.split(" ");
          				LinkedList<String> input=new LinkedList<String>();
          				for(int i = 0; i < split.length; i++){
          					input.add(split[i]);
          				}
          				params.add(new Object[] {expression,input,Boolean.valueOf(evaluationResult)});//new dadosTeste(expression,input,Boolean.valueOf(evaluationResult)));
          				
          			
          				br.readLine();
          			}
          		} catch (Exception e) {
          		
          			e.printStackTrace();
          		}
                  
                  
                  
                 
          return params;
     }  
	        

	@Test
	public void test1()
	{
		
		dado.getInput();
		 boolean res=resultados(dado.getRegex(),dado.getInput());
		 assertEquals(dado.isResult(),res);
	}
	

	
	

	

}